package com.yuma.lock;

import java.util.concurrent.TimeUnit;

public class DiedLock {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();


    public static void main(String[] args) {

        new Thread(()->{
            synchronized (lock1) {
                System.out.println("A 使用了第一把锁进入 1");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock2){
                    System.out.println("A 使用了第二把锁进入 2");
                }
            }
        }).start();

        new Thread(()->{
            synchronized (lock2) {
                System.out.println("B 使用了第2把锁进入 1");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock1){
                    System.out.println("B 使用了第1把锁进入 2");
                }
            }
        }).start();
    }
}
